package com.ui;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bean.HomeArticleList;
import com.ddd.wandemo.R;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class TestArticleRvAdapter extends RecyclerView.Adapter<TestArticleRvAdapter.TestArticleViewHolder> {
    private List<HomeArticleList.DatasBean> dataList = new ArrayList<>();
    private Context mContext;

    public TestArticleRvAdapter(Context context) {
        mContext = context;
    }

    @NonNull
    @Override
    public TestArticleRvAdapter.TestArticleViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new TestArticleViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_home_article,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull TestArticleRvAdapter.TestArticleViewHolder holder, int position) {
        holder.tvAuthor.setText(dataList.get(position).getAuthor());
        holder.tvChapterName.setText(dataList.get(position).getSuperChapterName());
        holder.tvTime.setText(new SimpleDateFormat("yyyy-MM-dd").format(new Date(dataList.get(position).getPublishTime())));
        holder.tvTitle.setText(dataList.get(position).getTitle());
    }

    @Override
    public int getItemCount() {
        return dataList == null ? 0 : dataList.size();
    }

    public class TestArticleViewHolder extends RecyclerView.ViewHolder {
        private TextView tvAuthor ,tvTime,tvTitle,tvChapterName;
        public TestArticleViewHolder(View itemView) {
            super(itemView);
            tvAuthor = itemView.findViewById(R.id.tv_article_author);
            tvTime = itemView.findViewById(R.id.tv_article_time);
            tvTitle = itemView.findViewById(R.id.tv_article_title);
            tvChapterName = itemView.findViewById(R.id.tv_article_chapter);
        }
    }


    public void setData(List<HomeArticleList.DatasBean> data) {
        if (dataList != null) {
            dataList.clear();
            dataList.addAll(data);
        }
        notifyDataSetChanged();
    }

    public void addData(List<HomeArticleList.DatasBean> data) {
        dataList.addAll(dataList.size(), data);
        notifyDataSetChanged();
    }
}
